//
//  Refresh.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/6/11.
//

import Foundation
import UIKit
import RxSwift

// MARK: - 配置上拉下拉
public extension Reactive where Base: Refreshable {
    
    func updateRefresh<T: RefreshControllable & ViewModelProtocol >(_ vm: T, scrollview: UIScrollView, bag: DisposeBag, refreshConfig config: ScrollViewRefreshEnum) {
        
        refresh(vm, scrollview).subscribe(onNext: {
            switch $0 {
            case .header:
                vm.refresh()
            case .footer:
                vm.pullUp()
            }
        }).disposed(by: bag)
        
        switch config {
        case .none:
            vm.rx.refreshStatus.onNext(.hiddenHeader)
            vm.rx.refreshStatus.onNext(.hiddenFooter)
        case .header:
            vm.rx.refreshStatus.onNext(.hiddenFooter)
        case .footer:
            vm.rx.refreshStatus.onNext(.hiddenHeader)
        case .all: break
        }
        
    }
}
